/*
 * Copyright (c) 2015—2030 GantSoftware.Co.Ltd. All rights reserved.
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * is not allowed to be distributed or copied without the license from
 * GantSoftware.Co.Ltd. Please contact the company for more information.
 */
package com.solver.demo;

import com.solver.demo.rscoket.v2.client.V2RSocketClient;
import com.solver.demo.rscoket.v1.V1RSocketClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import reactor.core.publisher.Flux;

import java.time.Duration;
import java.util.List;

@SpringBootApplication
@Slf4j
@EnableAsync
public class DemoApp {
    public static void main(String[] args) {
        SpringApplication.run(DemoApp.class, args);
    }

    @Async // 需在启动类添加 @EnableAsync
    public void handleResponses(Flux<String> result) {
        result
                .doOnNext(response -> log.info("Async Received: {}", response))
                .subscribe();
    }

    @Bean
    public CommandLineRunner commandLineRunner() {
        return args -> {
            log.info("Starting demo");
//            SmtSolverUtil.solverContext();
            V1RSocketClient.test();
            List<String> contents = List.of("Hello", "World", "RSocket", "Solver");
            Flux<String> result =
                    V2RSocketClient.sendGroupedMessages(contents, Duration.ofSeconds(5));
            handleResponses(result); // 提交到异步线程
            log.info("end demo");
        };
    }

    @Autowired
    V1RSocketClient V1RSocketClient;
    @Autowired
    V2RSocketClient V2RSocketClient;
}
